//
//  YZMAppHelper.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/21.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMAppHelper.h"
#import <UIKit/UIKit.h>
#import "YZMAlertView.h"
#import "YZMAppStyle.h"
#import "YZMControllerManager.h"
#import "MBProgressHUD+Utils.h"
#import "YZMSeverTimeService.h"
#import <DateTools/DateTools.h>

#define APPSTORE_URL @"itms-apps://itunes.apple.com/us/app/ying-ce-e/id1112044972?l=zh&ls=1&mt=8"

@implementation YZMAppHelper

+ (void)callServicePhone{
    
    NSURL *telUrl = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", [YZMAppHelper fetchPhoneNumber]]];
    
    if (![[UIApplication sharedApplication] canOpenURL: telUrl]) {
        
        [MBProgressHUD showMessage: @"该设备无电话功能"];
        return;
    }
    
    YZMAlertView *alert = [[YZMAlertView alloc] init];
    alert.titleLabel.text = @"客服热线";
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 10;
    NSString *timeStr1 = @"\n服务时间09:00-17:30\n";
    NSString *timeStr2 = @"\n服务时间17:30-21:00";
    NSString *contentStr = [[sYZMFixedTelephoneForClientService stringByAppendingString:timeStr1] stringByAppendingString:[sYZMFixedPhoneForClientService stringByAppendingString:timeStr2]];
    NSString *forStr = [[sYZMFixedTelephoneForClientService stringByAppendingString:timeStr1]stringByAppendingString:sYZMFixedPhoneForClientService];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString: contentStr attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize: 20], NSForegroundColorAttributeName: [YZMAppStyle colorForBlackText],NSParagraphStyleAttributeName: style}];
    
    [attrStr addAttributes:@{NSFontAttributeName: [YZMAppStyle mediumSizeFont], NSForegroundColorAttributeName: [YZMAppStyle colorForLightGrayText]} range:NSMakeRange(sYZMFixedTelephoneForClientService.length, timeStr1.length)];
    [attrStr addAttributes:@{NSFontAttributeName: [YZMAppStyle mediumSizeFont], NSForegroundColorAttributeName: [YZMAppStyle colorForLightGrayText]} range:NSMakeRange(forStr.length, timeStr2.length)];
    alert.contentLabel.attributedText = attrStr;
    alert.contentLabel.textAlignment = NSTextAlignmentCenter;
    [alert showInView:[YZMControllerManager sharedManager].rootTabBarCtrl.view];
    
    alert.confirmHandle = ^(){
        
        [[UIApplication sharedApplication] openURL: telUrl];
        return YES;
    };
}

+ (void)showEvaluate{
    
    /*为我评价，跳转苹果商店*/
    NSURL *url = [NSURL URLWithString:APPSTORE_URL];
    [[UIApplication sharedApplication]openURL:url];
}

+ (void)changepayZFAccount {
    
    NSURL *telUrl = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", [YZMAppHelper fetchPhoneNumber]]];
    if (![[UIApplication sharedApplication] canOpenURL: telUrl]) {
        
        [MBProgressHUD showMessage: @"该设备无电话功能"];
        return;
    }
    
    YZMAlertView *alertView = [[YZMAlertView alloc] init];
    
    alertView.contentLabel.text = [@"如需更换或解绑支付宝账号，请联系客服电话 " stringByAppendingString:[YZMAppHelper fetchPhoneNumber]];
    alertView.contentLabel.textAlignment = NSTextAlignmentCenter;
    [alertView.sureButton setTitle:@"拨打" forState:UIControlStateNormal];
    alertView.confirmHandle = ^(){
            
        [[UIApplication sharedApplication] openURL: telUrl];
        return YES;
    };
    [alertView show];
}

+ (NSString *)fetchPhoneNumber {
    
    NSDate *severDate = [YZMSeverTimeService sharedService].severDate;
    NSInteger year = [[severDate formattedDateWithFormat: @"yyyy"] integerValue];
    NSInteger month = [[severDate formattedDateWithFormat: @"MM"] integerValue];
    NSInteger day = [[severDate formattedDateWithFormat: @"dd"] integerValue];
    
    NSDate *beginDate = [NSDate dateWithYear:year month:month day:day hour:17 minute:30 second:59];
    NSDate *endDate = [NSDate dateWithYear:year month:month day:day hour:21 minute:0 second:59];
    DTTimePeriod *timePeriod = [DTTimePeriod timePeriodWithStartDate:beginDate endDate:endDate];
    
    return [timePeriod containsDate:severDate interval:DTTimePeriodIntervalClosed] ? sYZMFixedPhoneForClientService : sYZMFixedTelephoneForClientService;
}

+ (NSString *)version{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

+ (NSString *)buildVersion{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}

+ (double)systemVersion{
    return   [[UIDevice currentDevice] systemVersion].doubleValue;
}

@end
